# 第一题、给定一个列表, 根据`grade`来排序
from pprint import pprint
classes = [
    {"name": "n_1", "age": 24, "grade": "A"},
    {"name": "n_2", "age": 23, "grade": "B"},
    {"name": "n_3", "age": 28, "grade": "A"},
    {"name": "n_4", "age": 24, "grade": "A"},
    {"name": "n_5", "age": 25, "grade": "C"},
    {"name": "n_6", "age": 21, "grade": "D"},
    {"name": "n_7", "age": 27, "grade": "A"},
]
print("原始数据：")
pprint(classes)

s_grade = sorted(classes, key=lambda x: x.get('grade') )   #通过lambda关键字来声明匿名函数
print("\n按grade排序")
pprint(s_grade)

# 第二题、通过filter语句来筛选出Grade为A的同学
g = filter(lambda x:x.get('grade')=='A',classes)   #filter(函数, 可迭代对象)
print("\ngrade为A的同学有：")
pprint(list(g))

#第三题、通过map函数将上述同学的age + 1(对原数据结构有影响, 尽量不要用lambda)
def add(dict):
    dict["age"] += 1   #对 age 加1;参数是一个字典
m = map(add, classes)  #classes中的每一个元素就是一个字典
list(m)
print("\n改变年龄之后：")
pprint(classes)

#第四题、使用递归函数重构斐波那契函数
from pprint import pprint
def fact(n):
    if n <= 1:
        return n
    else:
        return fact(n-1) + fact(n-2)

for i in range(0, 10):
    print(fact(i), end=', ')